package com.mea.site.module.sys.controller;

import cn.hutool.core.util.URLUtil;
import com.feilong.core.Validator;
import com.mea.site.common.response.ResponseEntity;
import com.mea.site.common.utils.ShiroUtils;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.shiro.session.Session;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.Random;

/**
 * Created by lenovo on 2018/2/26.
 */
@Controller
@RequestMapping(value = "/common")
@Slf4j
public class CommonController {

    public static final String VALIDATE_CODE = "validateCode";

    private int w = 70;
    private int h = 26;


    public static boolean validate(HttpServletRequest request, String validateCode) {
        String code = (String) request.getSession().getAttribute(VALIDATE_CODE);
        return validateCode.toUpperCase().equals(code);
    }

    /**
     * 获取验证码
     *
     * @param request
     * @param response
     */
    @GetMapping(value = "code.jpg")
    public void validateCode(HttpServletRequest request, HttpServletResponse response) {
        try {
            createImage(request, response);
        } catch (IOException e) {
            log.error("获取验证码失败:", e);
        }
    }

    /**
     * 校验验证码
     *
     * @param request
     * @param code
     * @return
     */
    @PostMapping(value = "validateCode")
    @ResponseBody
    public ResponseEntity validateCode(HttpServletRequest request, String code) {
        if (validate(request, code)) {
            return ResponseEntity.build().OK();
        } else {
            return ResponseEntity.build().fail();
        }
    }

    public static boolean validateCode(String validateCode) {
        Session session = ShiroUtils.getSession();
        String code = (String) session.getAttribute(VALIDATE_CODE);
        return validateCode.toUpperCase().equals(code);
    }

    private void createImage(HttpServletRequest request,
                             HttpServletResponse response) throws IOException {

        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");

		/*
         * 得到参数高，宽，都为数字时，则使用设置高宽，否则使用默认值
		 */
        String width = request.getParameter("width");
        String height = request.getParameter("height");
        if (StringUtils.isNumeric(width) && StringUtils.isNumeric(height)) {
            w = NumberUtils.toInt(width);
            h = NumberUtils.toInt(height);
        }

        BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();

		/*
         * 生成背景
		 */
        createBackground(g);

		/*
         * 生成字符
		 */
        String s = createCharacter(g);
        request.getSession().setAttribute(VALIDATE_CODE, s);

        g.dispose();
        OutputStream out = response.getOutputStream();
        ImageIO.write(image, "JPEG", out);
        out.close();

    }

    private Color getRandColor(int fc, int bc) {
        int f = fc;
        int b = bc;
        Random random = new Random();
        if (f > 255) {
            f = 255;
        }
        if (b > 255) {
            b = 255;
        }
        return new Color(f + random.nextInt(b - f), f + random.nextInt(b - f), f + random.nextInt(b - f));
    }

    private void createBackground(Graphics g) {
        // 填充背景
        g.setColor(getRandColor(220, 250));
        g.fillRect(0, 0, w, h);
        // 加入干扰线条
        for (int i = 0; i < 8; i++) {
            g.setColor(getRandColor(40, 150));
            Random random = new Random();
            int x = random.nextInt(w);
            int y = random.nextInt(h);
            int x1 = random.nextInt(w);
            int y1 = random.nextInt(h);
            g.drawLine(x, y, x1, y1);
        }
    }

    private String createCharacter(Graphics g) {
        char[] codeSeq = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J',
                'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
                'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9'};
        String[] fontTypes = {"Arial", "Arial Black", "AvantGarde Bk BT", "Calibri"};
        Random random = new Random();
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            String r = String.valueOf(codeSeq[random.nextInt(codeSeq.length)]);//random.nextInt(10));
            g.setColor(new Color(50 + random.nextInt(100), 50 + random.nextInt(100), 50 + random.nextInt(100)));
            g.setFont(new Font(fontTypes[random.nextInt(fontTypes.length)], Font.BOLD, 26));
            g.drawString(r, 15 * i + 5, 19 + random.nextInt(8));
//			g.drawString(r, i*w/4, h-5);
            s.append(r);
        }
        return s.toString();
    }

    /**
     * 跳转树形结构
     *
     * @param url
     * @return
     */
    @RequestMapping(value = "tree")
    public String tree(String url, String checked, String extId,Integer index,String isOpen, String idN, Model model) {
        model.addAttribute("url", Validator.isNotNullOrEmpty(url) == true ? URLUtil.decode(url) : "");
        model.addAttribute("checked", checked);
        model.addAttribute("extId", extId);
        model.addAttribute("idN", idN);
        model.addAttribute("index", index);
        model.addAttribute("isOpen", isOpen);
        return "/common/tpl/treeselect";
    }
}
